package com.fieldchain.ex.fa100.ui.personal.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fieldchain.ex.fa100.R;
import com.fieldchain.ex.fa100.access.PlantWorldActivity;
import com.fieldchain.ex.fa100.base.MyBaseActivity;
import com.fieldchain.ex.fa100.mvp.presenter.RegistPresenter;
import com.fieldchain.ex.fa100.mvp.view.RegistView;

import com.fieldchain.ex.fa100.utils.common.InputAnimation;
import com.fieldchain.ex.fa100.utils.screen.MyToast;

/**
 * Created by biyunlong
 * Email: 1225198434@qq.com
 * Data:  2018/9/7
 */
public class RegistNextActivity extends MyBaseActivity<RegistPresenter, RegistView> implements View.OnClickListener, RegistView {
    private RelativeLayout reaBack;
    private ImageView ivBac;
    private RelativeLayout reaPwd;
    private EditText etPwd;
    private RelativeLayout reaResumpwd;
    private EditText etResumpwd;
    private RelativeLayout reaInvite;
    private EditText etInvite;
    private CheckBox checkbox;
    private TextView tvService;
    private TextView tvSure;
    private LinearLayout linBottom;


    private String name, imagCode, smsCode;
    private InputAnimation animation;

    @Override
    public void findView() {
        reaBack = findViewById(R.id.rea_back);
        ivBac = findViewById(R.id.iv_bac);
        reaPwd = findViewById(R.id.rea_pwd);
        etPwd = findViewById(R.id.et_pwd);
        reaResumpwd = findViewById(R.id.rea_resumpwd);
        etResumpwd = findViewById(R.id.et_resumpwd);
        reaInvite = findViewById(R.id.rea_invite);
        etInvite = findViewById(R.id.et_invite);
        checkbox = findViewById(R.id.checkbox);
        tvService = findViewById(R.id.tv_service);
        tvSure = findViewById(R.id.tv_sure);
        linBottom = findViewById(R.id.lin_bottom);
    }

    @Override
    public void setValue() {
        setInput();

        reaBack.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        tvService.setOnClickListener(this);

        imagCode = getIntent().getStringExtra("imagcode");
        smsCode = getIntent().getStringExtra("smsCode");
        name = getIntent().getStringExtra("name");

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rea_back:
                finish();
                break;
            case R.id.tv_sure:
                String pwd = etPwd.getText().toString();
                String resumPwd = etResumpwd.getText().toString();
                String invite = etInvite.getText().toString();
                int length = pwd.length();
                if (!(length >= 6 && length <= 20)) {
                    MyToast.show("密码格式为6-20位数字字母字符组合");
                    return;
                }
                if (!pwd.equals(resumPwd)) {
                    MyToast.show("两次密码不一致");
                    return;
                }
                if (!checkbox.isChecked()) {
                    MyToast.show("请先阅读用户协议");
                    return;
                }
                presenter.regist(name, smsCode, pwd, invite);
                break;
            case R.id.tv_service:
                startActivity(new Intent(mContext, PlantWorldActivity.class));
                break;
        }
    }

    public void setInput() {
        animation = new InputAnimation();
        animation.attachView((Activity) mContext, 40, etInvite, linBottom, ivBac);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        animation.detachView();
        animation = null;
    }

    @Override
    public int getLayout() {
        return R.layout.activity_nextregist;
    }


    @Override
    public void registResult(int type) {
        if (type == 200) {
            finish();
        }
    }

    @Override
    public void verImage(Bitmap bit) {

    }
}
